'''
Created on 14.12.2009

@author: claudiugh
'''

from apriori import AprioriRulesGenerator 

class DirectHashingGenerator(AprioriRulesGenerator):
    
    #override 
    def _get_initial_candidates(self):
        pregen_itemsets = {}
        for transaction in self.db: 
            items = tuple(transaction)
            tlen = len(items)
            for i in xrange(tlen):
                for j in xrange(i + 1, tlen):
                    if items[i] < items[j]: 
                        pair = tuple([items[i], items[j]])
                    else: 
                        pair = tuple([items[j], items[i]])
                    pregen_itemsets.setdefault(pair, 0)
                    pregen_itemsets[pair] += 1 
        # we have 2-itemsets already generated
        candidates = [] 
        for pair in pregen_itemsets: 
            if float(pregen_itemsets[pair]) / float(len(self.db)) >= self.minsupp:
                candidates.append(pair)
        return candidates 
        